<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Role_model extends CI_Model{
	function __construct() {
		parent::__construct();
	}
	
	function add_role($clause){
                $data_fungsi = $clause['fungsi'];
                unset($clause['fungsi']);
                unset($clause['list_fungsi_length']);
                $this->db->insert('role',$clause);
                $role_id = $this->db->insert_id();
                $temp_array = array_keys($data_fungsi);
                for($i=0;$i<count($data_fungsi);$i++)
                {
                    for($j=0;$j<count($data_fungsi[$temp_array[$i]]);$j++)
                    {
                        $clause_fungsi['role_id'] = $role_id;
                        $clause_fungsi['fungsi_id'] = $temp_array[$i];
                        $clause_fungsi['aksi_id'] = $data_fungsi[$temp_array[$i]][$j];
                        $this->add_role_fungsi($clause_fungsi);
                    }
                }
		
//                die($this->db->last_query());
		return $this->db->insert_id();
	}
        
        function add_role_fungsi($data)
        {
            $this->db->insert('role_fungsi',$data);
//                die($this->db->last_query());
		return $this->db->insert_id();
        }
        
        function delete_role_fungsi_by_id($id)
        {
            $this->db->delete('role_fungsi',array('role_id'=>$id));
        }
        
        function is_role_aksi_fungsi($data)
        {
            $query = $this->db->get_where('role_fungsi',$data);
		if($query->num_rows>0){
			return true;
		}else{
			return FALSE;
		}
        }
	
	
	function update_role($data){
		$id = $data['id'];
		unset ($data['id']);
                $data_fungsi = $data['fungsi'];
                unset($data['fungsi']);
                unset($data['list_fungsi_length']);
		$this->db->where('id',$id);
		$this->db->update('role',$data);
                $this->delete_role_fungsi_by_id($id);
                $temp_array = array_keys($data_fungsi);
                for($i=0;$i<count($data_fungsi);$i++)
                {
                    for($j=0;$j<count($data_fungsi[$temp_array[$i]]);$j++)
                    {
                        $clause_fungsi['role_id'] = $id;
                        $clause_fungsi['fungsi_id'] = $temp_array[$i];
                        $clause_fungsi['aksi_id'] = $data_fungsi[$temp_array[$i]][$j];
                        $this->add_role_fungsi($clause_fungsi);
                    }
                }
	}
	
	function get_role($id){
		$query = $this->db->get_where('role',array('id'=>$id));
		if($query->num_rows>0){
			return $query->row_array();
		}else{
			return FALSE;
		}
	}
	
	function get_all_role(){
		$query = $this->db->get('role');
		return $query->result_array();
	}
        
        function delete_role($id)
        {
            $this->db->delete('role',array('id' => $id));
            $this->delete_role_fungsi_by_id($id);
        }
	
}

/** End of file */